

/**
 * Created with IntelliJ IDEA.
 * Description: 加减乘除运算
 * User: 86183
 * Date: 2022-10-27
 * Time: 21:55
 */
//定义一个接口Computer
interface Computer{
     int computer(int n,int m);
}
//加减乘除都实现该接口
//加法类
class Add implements Computer{
    @Override
    public int computer(int n, int m) {
        System.out.print(n+ "+" + m +"=");
        return n+m;
    }
}
//减法类
class Minus implements Computer{
    @Override
    public int computer(int n, int m) {
        System.out.print(n+ "-" + m +"=");
        return n-m;
    }
}
//乘法类
class Ride implements Computer{

    @Override
    public int computer(int n, int m) {
        System.out.print(n+ "*" + m +"=");
        return n*m;
    }
}
//除法类
class Division implements Computer{
    @Override
    public int computer(int n, int m) {
        //当分母为零时，抛出算术异常
        if(m == 0){
            throw new ArithmeticException("分母为0");
        }
        System.out.print(n+ "/" + m +"=");
        return n/m;
    }
}
//UseComputer类
class UseComputer{
    public void useCom(Computer com,int one,int two){
       int count =  com.computer(one,two);
        System.out.print(count);
    }
}
public class Tak {
    public static void main(String[] args) {
        UseComputer use = new UseComputer();//实例化UseComputer类
        //用try-catch解决抛出的异常
        try {
            use.useCom(new Division(),7,0);
        }catch (ArithmeticException e){
            e.printStackTrace();
        }
        System.out.println();
        use.useCom(new Add(),7,0);
        System.out.println();
        use.useCom(new Minus(),7,0);
        System.out.println();
        use.useCom(new Ride(),7,0);



    }
}
